<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .phone {
            width: 300px;
            height: 500px;
            margin: 50px auto;
            border: 1px solid #333;
            position: relative;
        }

        .slideBar {
            width: 100%;
            height: 50px;
            background-color: grey;
            position: absolute;
            top: 60%;
            left: 0;
        }

        .ball {
            width: 50px;
            height: 50px;
            background-color: #6ff;
            border-radius: 50%;
            position: absolute;
            left: 0px;
            top: 0;
        }
    </style>
</head>

<body>
    <div class="phone">
        <div class="slideBar">
            <div class="ball"></div>
        </div>
    </div>
</body>
<script>

    var phone = document.getElementsByClassName("phone")[0];
    var slideBar = document.getElementsByClassName("slideBar")[0];
    var ball = document.getElementsByClassName("ball")[0];

    var maxLeft = slideBar.clientWidth - ball.clientWidth;

    ball.onmousedown = function (e) {
        var e = e || window.event;
        var posX = e.offsetX;
        var posY = e.offsetY;
        console.log(posX, posY);

        var x = null;

        document.onmousemove = function (e) {
            var e = e || window.event;
            x = e.pageX - phone.offsetLeft - posX;

            if (x < 0) x = 0;
            if (x > maxLeft) x = maxLeft;

            ball.style.left = x + "px";

            // 验证1: 移动过程中 到达最右边 -> 解锁
            if (x == maxLeft) {
                location.href = "https://www.baidu.com";
            }

        }

        document.onmouseup = function () {
            document.onmousemove = null;

            // 验证2: 松开时 如果没有到达最右边 -> 回到原位置
            if (x == maxLeft) {
                location.href = "https://www.baidu.com";
            } else {
                ball.style.left = 0;
            }
        }

    }





</script>

</html>